package wwl.lsf.hellospringboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title
 * @Author wangwenliang
 * @Date 2022/2/22
 * @Description
 */
@RestController
@Slf4j
public class HaHaControler {

    private ThreadLocal<Integer> currentUser = ThreadLocal.withInitial(() -> null);

    @GetMapping("/wrong")
    public Map wrong(@RequestParam("userId") Integer userId) {
//设置用户信息之前先查询一次ThreadLocal中的用户信息
        String before = Thread.currentThread().getName() + ":" + currentUser.get();
//设置用户信息到ThreadLocal
        currentUser.set(userId);
//设置用户信息之后再查询一次ThreadLocal中的用户信息
        String after = Thread.currentThread().getName() + ":" + currentUser.get();
        //汇总输出两次查询结果
        Map result = new HashMap();
        result.put("before", before);
        result.put("after", after);
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/right")
    public Map right(@RequestParam("userId") Integer userId) {
        Map result;
        try {
//设置用户信息之前先查询一次ThreadLocal中的用户信息
            String before = Thread.currentThread().getName() + ":" + currentUser.get();
//设置用户信息到ThreadLocal
            currentUser.set(userId);
//设置用户信息之后再查询一次ThreadLocal中的用户信息
            String after = Thread.currentThread().getName() + ":" + currentUser.get();
            //汇总输出两次查询结果
            result = new HashMap();
            result.put("before", before);
            result.put("after", after);
            log.info("result:{}", result);
        } finally {
            currentUser.remove();
        }
        return result;
    }


}
